package org.jef.socket.thread;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @version 1.0
 * @description: 回调方法执行器
 * @author: ChengZhi 0336
 * @create: 2024-10-15 17:57
 **/
public class SingleFuncbleExector {

    /**
     * 记录任务和类型
     */
    private static final Map<String, Funcable> tasks = new ConcurrentHashMap<>();

    /**
     * 添加任务
     * @param key
     * @param task
     */
    public static void putTask(String key, Runnable task) {
        tasks.put(key, (Funcable) task);
    }

    /**
     * 运行任务
     * @param key
     * @return
     */
    public static Object runTask(String key) {
        Funcable funcable = tasks.get(key);
        Object result = null;
        if (funcable != null) {
            result = funcable.execute();
            tasks.remove(key);
        }

        return result;
    }
}
